﻿using AnQinManagermentBackendAPI.Common;
using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.Project;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.Project;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class ProjectService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public ProjectService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取项目列表
         */
        public async Task<RequestResponseModel<PageResult<ProjectView>>> GetProjectList(ProjectSearchDTO projectSearchDTO)
        {
            //筛选
            var query = _context.ProjectView
                .WhereIF(a => a.Code.Contains(projectSearchDTO.Code), !string.IsNullOrEmpty(projectSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(projectSearchDTO.Name), !string.IsNullOrEmpty(projectSearchDTO.Name))
                .WhereIF(a => a.StaffId == projectSearchDTO.StaffId, !string.IsNullOrEmpty(projectSearchDTO.StaffId))
                .WhereIF(a => a.CustomerId == projectSearchDTO.CustomerId, !string.IsNullOrEmpty(projectSearchDTO.CustomerId))
                .WhereIF(a => a.State == projectSearchDTO.State, projectSearchDTO.State != null)
                ;

            //返回列表
            var projectList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(projectSearchDTO.CurrentPage, projectSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<ProjectView>(projectList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增项目
         */
        public async Task<RequestResponseModel<string>> AddProject(ProjectAddDTO projectAddDTO)
        {
            //操作数据
            DateTime dateTime = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var project = new ProjectModel
            {
                Id = guid.ToString(),
                Code = projectAddDTO.Code,
                Name = projectAddDTO.Name,
                StaffId = projectAddDTO.StaffId,
                CustomerId = projectAddDTO.CustomerId,
                State = StateEnum.ProjectStateEnum.待确认,
                File = projectAddDTO.File,
                Remark = projectAddDTO.Remark,
                CreateTime = dateTime
            };

            //新增项目
            _context.Project.Add(project);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 编辑项目
         */
        public async Task<RequestResponseModel<string>> PutProject(ProjectUpdateDTO projectUpdateDTO)
        {
            //查找数据
            var query = await _context.Project.SingleOrDefaultAsync(a => a.Id == projectUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", projectUpdateDTO.Id);
            };

            //生成新数据
            query.Name = projectUpdateDTO.Name;
            query.StaffId = projectUpdateDTO.StaffId;
            query.CustomerId = projectUpdateDTO.CustomerId;
            query.File = projectUpdateDTO.File;
            query.Remark = projectUpdateDTO.Remark;

            //更新项目表
            _context.Project.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", projectUpdateDTO.Id);
        }

        /**
         * 删除项目
         */
        public async Task<RequestResponseModel<string>> DeleteProject(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.Project.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.Project.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 申请售后
         */
        public async Task<RequestResponseModel<string>> AfterSaleProject(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.Project.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //更新状态
            query.State = StateEnum.ProjectStateEnum.售后;

            //保存库
            _context.Project.Update(query);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 项目下拉列表 - 开发管理
         */
        public async Task<RequestResponseModel<List<ProjectSelectView>>> DevelopmentSelectProject()
        {
            //返回状态为开发中和售后的项目
            var query = await _context.ProjectSelectView
                .Where(a => a.State == StateEnum.ProjectStateEnum.开发中 || a.State == StateEnum.ProjectStateEnum.售后)
                .ToListAsync();

            return RequestResponse.CreateResponse(200, "查询成功", query);
        }

        /**
         * 项目下拉列表 - 实施管理
         */
        public async Task<RequestResponseModel<List<ProjectSelectView>>> ImplementSelectProject()
        {
            //返回状态为开发中和售后的项目
            var query = await _context.ProjectSelectView
                .Where(a => a.State == StateEnum.ProjectStateEnum.待实施)
                .ToListAsync();

            return RequestResponse.CreateResponse(200, "查询成功", query);
        }
    }
}
